package bs.app.Client;

import android.graphics.Color;
import android.os.Build;
import bs.app.Component.TextField;
import bs.app.Constant.CConfig;
import bs.app.Constant.KeyConstant;
import bs.app.Main.CCanvas;
import bs.app.Position.CPosition;
import bs.app.Res.CRess;
import bs.app.android.Graphics;

public class SearchPage {
	
	public TextField textfield;
	public int xTextField, yTextField, wTextField, hTextField;
	public int timeForLoading = 50;
	public String Link;
	public String Title = "Tìm kiếm";
	public final String s_keyboard[] = { "0", "1", "2", "3", "4", "5", "6",
			"7", "8", "9", "q", "w", "e", "r", "t", "y", "u", "i", "o", "p",
			"a", "s", "d", "f", "g", "h", "j", "k", "l", " ", "z", "x", "c",
			"v", "b", "n", "m", ",", ".", "?" };
	public final int iRow = 4;
	public final int iCol = 10;
	public int iH = 47;
	public int iW = 32;

	public SearchPage() {
		iH = CRess.keyboard.getHeight()/4;
		iW = CRess.keyboard.getWidth()/10;
		
	}

	public void init(String link) {
		
		
		wTextField = 132;
		hTextField = 16;
		
		xTextField = CCanvas.s_iWcanvas / 2 - wTextField / 2 - 9;
		yTextField = CCanvas.s_iHcanvas - CRess.keyboard.getHeight()
				- CConfig.HIEGHT_COMMNAND - hTextField - 6;
		
		if("GT-S5360".equals(Build.MODEL))
		{
			xTextField = xTextField + 16;
			yTextField = yTextField + 8;
			wTextField = 100;
		}
		
		textfield = new TextField();
		textfield.setBound(xTextField, yTextField, wTextField, hTextField);
		this.Link = link;
	}

	public void init(String link, String tilte) {
		wTextField = 132;
		hTextField = 16;
		xTextField = CCanvas.s_iWcanvas / 2 - wTextField / 2 - 9;
		yTextField = CCanvas.s_iHcanvas - CRess.keyboard.getHeight()
				- CConfig.HIEGHT_COMMNAND - hTextField - 6;
		
		if("GT-S5360".equals(Build.MODEL))
		{
			xTextField = xTextField + 16;
			yTextField = yTextField + 8;
			wTextField = 100;
		}
		
		textfield = new TextField();
		textfield.setBound(xTextField, yTextField, wTextField, hTextField);
		this.Link = link;
		this.Title = tilte;
	}

	public void Painttransparent(Graphics g) {
		// g.drawImage(isChangeLoadingState.imgtransparent,CCanvas.xTranslate,
		// CCanvas.yTranslate+25, Graphics.TOP|Graphics.LEFT);
	}

	public void painttextfield(Graphics g) {
		// g.drawImage(CRess.menubar,0, CCanvas.s_iHcanvas,
		// CPosition.ANCHOR_LEFT_TOP);
		if (CCanvas.s_iWcanvas > CRess.menubar.getWidth()) {
			g.drawImage(CRess.menubar, 0, CCanvas.s_iHcanvas,
					CPosition.ANCHOR_LEFT_BOTTOM);
			for (int i = CRess.menubar.getWidth(); i < CCanvas.s_iWcanvas
					+ CRess.footer_back.getWidth(); i += CRess.footer_back
					.getWidth())
				g.drawImage(CRess.footer_back, i, CCanvas.s_iHcanvas,
						CPosition.ANCHOR_LEFT_BOTTOM);

		} else {
			g.drawImage(CRess.menubar, 0, CCanvas.s_iHcanvas,
					CPosition.ANCHOR_LEFT_BOTTOM);

		}
		
		//g.drawImage(CRess.keyboard,(CCanvas.s_iWcanvas - CRess.keyboard.getWidth()) / 2,CCanvas.s_iHcanvas - CRess.keyboard.getHeight()- CConfig.HIEGHT_COMMNAND, CPosition.ANCHOR_LEFT_TOP);
		g.drawImage(CRess.keyboard,(CCanvas.s_iWcanvas - CRess.keyboard.getWidth()) / 2,CCanvas.s_iHcanvas - CRess.keyboard.getHeight()- CRess.menubar.getHeight(), CPosition.ANCHOR_LEFT_TOP);
		
		//g.drawImage(CRess.keyboard, 100,0, 0);
		textfield.paint(g, Title);
//		CCanvas.mediumfont.drawString(g, "Gửi", CCanvas.s_iWcanvas / 2
//				- CCanvas.mediumfont.stringWidth("Gửi") / 2, CCanvas.s_iHcanvas
//				- CConfig.FONT_HEIGHT_COMMAND, 0, 0xa1a6a1, 0x090b7a, true);
		
		CCanvas.mediumfont.drawString(g, "Gửi", CCanvas.s_iWcanvas / 2- CCanvas.mediumfont.stringWidth("Gửi") / 2,
				CCanvas.s_iHcanvas, CPosition.ANCHOR_LEFT_BOTTOM,
				0xa1a6a1, 0x090b7a, true);
		
		// debug
//		CCanvas.mediumfont.drawString(g, String.valueOf(CCanvas.s_iWcanvas), 0, 30, 0, Color.RED, Color.RED, true);
//		CCanvas.mediumfont.drawString(g, String.valueOf(CCanvas.s_iHcanvas), 50, 30, 0, Color.RED, Color.RED, true);
//		
//		CCanvas.mediumfont.drawString(g, "keyboard w " +String.valueOf(CRess.search.getWidth()), 0, 60, 0, Color.RED, Color.RED, true);
//		CCanvas.mediumfont.drawString(g, "keyboard h " +Build.MODEL, 50, 120, 0, Color.RED, Color.RED, true);
//		
		
		// paint command
		if (textfield.TextString.trim().equals("")) {
//			CCanvas.mediumfont.drawString(g, "Quay Lại", CCanvas.s_iWcanvas
//					- CCanvas.mediumfont.stringWidth("Quay Lại") - 3,
//					CCanvas.s_iHcanvas - CConfig.FONT_HEIGHT_COMMAND, 0,
//					0xa1a6a1, 0x090b7a, true);
			
			CCanvas.mediumfont.drawString(g, "Quay Lại", CCanvas.s_iWcanvas
					- CCanvas.mediumfont.stringWidth("Quay Lại") - 3,
					CCanvas.s_iHcanvas, CPosition.ANCHOR_LEFT_BOTTOM,
					0xa1a6a1, 0x090b7a, true);
			
		} else {
			
//			CCanvas.mediumfont.drawString(g, "Xóa", CCanvas.s_iWcanvas
//					- CCanvas.mediumfont.stringWidth("Xóa") - 3,
//					CCanvas.s_iHcanvas - CConfig.FONT_HEIGHT_COMMAND, 0,
//					0xa1a6a1, 0x090b7a, true);
			
			CCanvas.mediumfont.drawString(g, "Xóa", CCanvas.s_iWcanvas
					- CCanvas.mediumfont.stringWidth("Xóa") - 3,
					CCanvas.s_iHcanvas, CPosition.ANCHOR_LEFT_BOTTOM,
					0xa1a6a1, 0x090b7a, true);
		}
	}

	public int pointerItems(int x, int y) {

//		System.out.println("toa do  x " + x);
//		System.out.println("toa do  y " + y);
//
//		System.out.println("width " + CCanvas.s_iWcanvas);
//		System.out.println("highht " + CCanvas.s_iHcanvas);
//
//		System.out.println("width search" + CRess.keyboard.getWidth());
//		System.out.println("width search " + CRess.keyboard.getHeight());
//
//		System.out.println("HIEGHT_COMMNAND " + CConfig.HIEGHT_COMMNAND);

		int x0 = (CCanvas.s_iWcanvas - CRess.keyboard.getWidth()) / 2 + 4;
//		int x1 = (CCanvas.s_iWcanvas - CRess.keyboard.getWidth()) / 2
//				+ CCanvas.s_iWcanvas - 4;

		int x1 = x0 + CRess.keyboard.getWidth();
		
		int y0 = CCanvas.s_iHcanvas - CRess.keyboard.getHeight() + 4
				- CConfig.HIEGHT_COMMNAND;

//		int y1 = CCanvas.s_iHcanvas + CRess.keyboard.getHeight() - 4
//				- CConfig.HIEGHT_COMMNAND;

		int y1 = CCanvas.s_iHcanvas - CConfig.HIEGHT_COMMNAND;
				
		
//
//		System.out.println("iH " + iH);
//		System.out.println("iW " + iW);

		if (x >= x0 && x <= x1 && y >= y0 && y <= y1) {
			
//			System.out.println("x0 " + x0);
//			System.out.println("yo " + y0);
//
//			System.out.println("x1" + x1);
//			System.out.println("y1" + y1);
//			
			y = y - y0;
			x = x - x0;
			int iIndex = ((y + iH) / iH - 1) * iCol + ((x + iW) / iW - 1);
			if (iIndex >= 0 && iIndex < s_keyboard.length
					&& textfield.TextString.length() < TextField.maxTextLength) {
				textfield.TextString += s_keyboard[iIndex];
				textfield.caretPos++;
				textfield.widthCaret = 0;
			}
		}
		return -1;
	}

	public void pointerPressed(int px, int py) {
		if (CCanvas.isPointerPressed) // User PointerPressed
		{
			if (Pointer.SelectSoftKey(px, py) == 3) {
				CCanvas.keyPressed[KeyConstant.KEY_RIGHT_SOFTKEY] = true;
			} else if (Pointer.SelectSoftKey(px, py) == 1
					|| Pointer.SelectSoftKey(px, py) == 2) {
				CCanvas.keyPressed[KeyConstant.KEY_FIRE] = true;
			} else { // click into items
				pointerItems(px, py);
			}
		}
		CCanvas.resetPointerPress(); // User PointerReleased
	}

	public void UpdateKeyPress(boolean[] keyPressed, boolean[] keyHold,
			int currentAsccii[]) {
		update();
		if (CCanvas.s_iCurrentAsciiKeyPressed[0] >= 65
				&& CCanvas.s_iCurrentAsciiKeyPressed[0] <= 122) {
			textfield.keyPressedAscii(CCanvas.s_iCurrentAsciiKeyPressed[0]);
			CCanvas.s_iCurrentAsciiKeyPressed[0] = 0;
			CCanvas.resetkey();
			CCanvas.resetPointerPress();
			return;
		}
		// dung cho ban phim thuong
		if (CCanvas.currentkeycode >= 48 && CCanvas.currentkeycode <= 57) {
			for (int i = 48; i <= 57; i++) {
				if (CCanvas.keyPressed[i - 48]) {
					textfield.KeyPressAny(i);
				}
			}
		} else if (keyPressed[KeyConstant.KEY_FIRE]) // User PressKey Fire
		{
			if (!textfield.TextString.equals("")) {
				CCanvas.s_Instan.LoadingState.timeForLoading = 50;
				CCanvas.s_Instan.LoadingState.time = CCanvas.s_Instan.LoadingState.fixtime;
				CCanvas.CurrentScreen = CCanvas.isChangeLoadingState;
//				CCanvas.s_Instan.connect.setURL(Link + "&key="
//						+ textfield.TextString.trim() + "&page=" + 1
//						+ "&perpage=" + 5);
				
				CCanvas.s_Instan.connect.setURL(Link + "&key="
						+ textfield.TextString.trim() + "&page=" + 1
						+ "&perpage=" + 10);

				String link = (String) CCanvas.s_Instan.connect.URL
						.lastElement();

				// System.out.println(" liny Title " + Title);
				// System.out.println(" link ben ngoai " + Link);

				int begin = link.indexOf("&key=");
				if (begin > 0) {
					link = link.substring(0, begin); // get link
				}
				if (!Link.trim().equals(link) && Title.equals("Tìm kiếm")) {
					String saveLinkHome = (String) CCanvas.s_Instan.connect.URL
							.elementAt(0);
					//System.out.println("tanloc save link " + saveLinkHome);
					CCanvas.s_Instan.connect.URL.removeAllElements();
					CCanvas.s_Instan.connect.URL.addElement(saveLinkHome);
//					CCanvas.s_Instan.connect.URL.addElement(Link + "&key="
//							+ textfield.TextString.trim() + "&page=" + 1
//							+ "&perpage=" + 5);
					
					CCanvas.s_Instan.connect.URL.addElement(Link + "&key="
							+ textfield.TextString.trim() + "&page=" + 1
							+ "&perpage=" + 10);
					
//					String saveLinkName = (String) CCanvas.s_Instan.connect.Link
//							.lastElement();
	
					String saveLinkName = (String) CCanvas.s_Instan.connect.Link.elementAt(0);
					CCanvas.s_Instan.connect.Link.removeAllElements();
					CCanvas.s_Instan.connect.Link.addElement(saveLinkName);
				} else if (Title.equals("Tìm kiếm")) // cai nay la trung link
														// luc search
				{
					// /////// dung cho search trung muc
					String saveLinkHome = (String) CCanvas.s_Instan.connect.URL
							.elementAt(0);
					CCanvas.s_Instan.connect.URL.removeAllElements();
					CCanvas.s_Instan.connect.URL.addElement(saveLinkHome);
					System.out.println("tanloc save link " + saveLinkHome);
//					CCanvas.s_Instan.connect.URL.addElement(Link + "&key="
//							+ textfield.TextString.trim() + "&page=" + 1
//							+ "&perpage=" + 5);
//					
					CCanvas.s_Instan.connect.URL.addElement(Link + "&key="
							+ textfield.TextString.trim() + "&page=" + 1
							+ "&perpage=" + 10);
					
				} else {
					CCanvas.s_Instan.connect.Link.addElement("Hỗ trợ");
					CCanvas.s_Instan.connect.URL
							.addElement(CCanvas.s_Instan.connect.url);
				}

				CCanvas.s_Instan.connect.RequestServerGetData();
			}
		} else if (keyPressed[KeyConstant.KEY_LEFT_SOFTKEY]) {

		} else if (keyPressed[KeyConstant.KEY_RIGHT_SOFTKEY]) {
			if (textfield.caretPos > 0 && textfield.TextString.length() > 0) {
				textfield.TextString = textfield.TextString.substring(0,
						textfield.caretPos - 1)
						+ textfield.TextString.substring(textfield.caretPos,
								textfield.TextString.length());
				textfield.caretPos--;
			} else {
				CCanvas.CurrentScreen = CCanvas.SaveStatePrevious;
				if (CCanvas.s_Instan.connect.Link.size() > 0)
					CCanvas.s_Instan.connect.Link
							.removeElementAt(CCanvas.s_Instan.connect.Link
									.size() - 1);
			}
		}
		CCanvas.s_iCurrentAsciiKeyPressed[0] = 0;
		textfield.update();
		CCanvas.resetkey();
	}

	public void update() {

	}
}
